package com.sghd.fight.module.game.service.skill.effect;

import com.sghd.fight.module.game.config.SkillConfig;
import com.sghd.fight.module.game.core.Magic;
import com.sghd.fight.module.game.core.model.Unit;
import com.sghd.fight.module.game.service.room.IRoom;
import com.sghd.fight.module.game.service.skill.Context;
import com.sghd.pojo.fight.game.*;
import org.apache.commons.collections.MapUtils;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author frank
 * @date 2018-12-07 14:33
 * @desc 凋零
 **/
@Component
public class WitheredEffect implements Effect {

    @Override
    public void execute(Context context, IRoom room, Unit owner, Unit target, Map<String, Object> ctx, SkillReport skillReport, TargetReport targetReport, SkillConfig config) {
        Magic magic = target.getMagic();
        int count = magic.getMagics().values().stream().reduce(0, Integer::sum);
        int bet = count / MapUtils.getInteger(ctx, CONSUME);
        int attack = bet * MapUtils.getInteger(ctx, RECOVER);
        int level = owner.getSkillLevel(config.getId());
        Map<String, Object> extr2Level = MapUtils.getMap(ctx, String.valueOf(level));
        attack += MapUtils.getIntValue(extr2Level ,ATTACK);

        //计算公式
        int val = (int) (attack * getBet(owner, target));
        val = fixHp(owner, target, val, room, config);

        context.addValue(target, UnitValue.HP, -val);
        targetReport.addValue(ValueItem.valueOf(UnitValue.HP, -val));
        for (ChessType type : magic.getMagics().keySet()) {
            magic.update(type, -magic.getMagic(type));
        }

    }

    @Override
    public EffectType getType() {
        return EffectType.SK_WITHERED;
    }
}
